package com.java2007.controller.backend;


import com.java2007.controller.BaseServlet;
import com.java2007.factory.BeanFactory;
import com.java2007.pojo.Table;
import com.java2007.service.TableService;
import org.apache.commons.beanutils.BeanUtils;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * 餐桌Controller
 */
@WebServlet(name = "TableController" ,value = "/table")
public class TableController extends BaseServlet {

    private TableService tableService = (TableService) BeanFactory
            .getBean("com.java2007.service.impl.TableServiceImpl");
    /**
     * 查询所有餐桌
     * @param request
     * @param response
     * @return
     */
    public String findAll(HttpServletRequest request, HttpServletResponse response) {
        try {
            List<Table> tables = tableService.findAll();
            request.setAttribute("tables",tables);
            return "forward:backend/detail/table/table-list.jsp";

        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("errorMsg", "服务器正忙");
            return "forward:backend/index.jsp";
        }
    }
    /**
     * 通过餐桌名字模糊查询
     * @param request
     * @param response
     * @return
     */
    public String findByTableName(HttpServletRequest request, HttpServletResponse response){
        //关键字
        String keyword = request.getParameter("keyword");
        try {
            List<Table> tables = tableService.selectByTableName(keyword);
            request.setAttribute("tables", tables);
            return "forward:backend/detail/table/table-list.jsp";
        } catch (SQLException e) {
            e.printStackTrace();
            request.setAttribute("errorMsg", "服务器正忙");
            return "forward:backend/index.jsp";
        }
    }

    /**
     * 增加餐桌
     *
     * @param request
     * @param response
     * @return
     */
    public String save(HttpServletRequest request, HttpServletResponse response) {
        try {
            Map<String, String[]> parameterMap = request.getParameterMap();
            Table table = new Table();
            //遍历map<key, value>的key,如果foodType中有这个属性,就将value赋值
            BeanUtils.populate(table, parameterMap);
            tableService.addTable(table);
            //转发
            return "redirect:table?method=findAll";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("errorMsg", "服务器正忙");
            return "forward:backend/index.jsp";
        }
    }
    /**
     * 退桌/预定
     *
     * @param request
     * @param response
     * @return
     */
    public String update(HttpServletRequest request, HttpServletResponse response) {
        try {
            String tableId = request.getParameter("tableId");
            Table table1 = tableService.selectByTableId(Integer.parseInt(tableId));

            tableService.updateByTableId(table1);
            //转发
            request.setAttribute("tables",table1);
            return "redirect:table?method=findAll";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("errorMsg", "服务器正忙");
            return "forward:backend/index.jsp";
        }
    }
    /**
     * 删除所选食物
     *
     * @param request
     * @param response
     * @return
     */
    public String delete(HttpServletRequest request, HttpServletResponse response) {
        try {
            String tableId = request.getParameter("tableId");
            tableService.deleteByTableId(Integer.parseInt(tableId));
            return "forward:table?method=findAll";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("errorMsg", "服务器正忙");
            return "forward:backend/index.jsp";
        }
    }
}
